/**
 * Created by Churcy on 2017/1/9.
 */
$(document).ready(function () {
});
window.onload = function () {
    APK.searchAPK();
}

var APK = {
    searchAPK: function () {
        $("#commit").on('click', function () {
                swal({
                        title: "确定提交么?",
                        text: "提交后，如有需要，请重新修改",
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: "确定, 提交!",
                        closeOnConfirm: false,
                        showLoaderOnConfirm: true,
                    },
                    function () {
                        setTimeout(function () {
                            var tempArray = new Array();
                            var index = 0;
                            var appKey = $('#APP_KEY').val();
                            $('input[name="appUpgradeFlag"]').each(function(){
                                var flag=$(this).val();
                                var id=$(this).attr('id');

                                tempArray[index] = new Array;
                                tempArray[index][0]=id;
                                tempArray[index][1]=flag;
                                index++;
                            });
                            console.log(tempArray);

                            var conditions = {
                                updateList:tempArray
                            }
                            $.ajax({
                                type: "POST",
                                data: conditions,
                                traditional: true,
                                url: PROJECT_CTX + "/apkVersion/updateAppUpgradeFlag",
                                dataType: "json",
                                success: function (data) {
                                    console.log("success");
                                }
                            })
                            swal({
                                    title: "已修改",
                                    text: "您选择的数据已修改!",
                                    type: "success"
                                },
                                function () {
                                    window.location.href = PROJECT_CTX + "/apkVersion/apk/index?appKey=" + appKey ;
                                });

                        }, 2000);
                    });
            }
        )
        ;
        console.log("searchAPK")
        $("#searchBTN").on('click', function () {
            var conditions = getCurrentConditions();
            conditions.pageNo = 1;
            ajaxSearchAPKList(conditions);
        });
        $("#resetBTN").on('click', function () {
            resetFn();
        });
        $("#checkAll").on("click", function () {
            var check = $(this).prop("checked");
            console.log(check);
            $(".appUpgradeFlag").prop("checked", check);
            if(check){
                $(".appUpgradeFlag").val(1);
            } else {
                $(".appUpgradeFlag").val(0);
            }
        })
        $(".appUpgradeFlag").on("click", function () {
            var button = true;
            $(".appUpgradeFlag").each(function () {
                if ($(this).prop("checked") == false) {
                    $(this).val(0);
                    button = false;
                } else {
                    $(this).val(1);
                }
            })
            if (button) {
                $("#checkAll").prop("checked", true);
            } else {
                $("#checkAll").prop("checked", false);
            }
        })


    }
}
function searchFn() {
    var conditions = getCurrentConditions();
    console.log("searchFN")
    var response = ajaxSearchAPKList(conditions);
}
//重置查询条件
function resetFn() {
    $('#version').val("");

}

function getCurrentConditions() {
    /*console.log("getCurrentConditions");
     var createTime = $('input[name="create_time"]').val();
     var startDate = createTime.substring(0, 10);
     var endDate = createTime.substring(11);
     var name = $('input[name="name"]').val();
     var status = $('select[name="status"]').val();

     var pageSize = $("#n").val();*/
    var version = $("#version").val();
    var pageNo = $("#p").val();
    var APP_KEY = $('#APP_KEY').val();

    var conditions = {
        /*startDate: startDate,
         endDate: endDate,
         name: name,
         pageSize: pageSize,
         status: status,*/
        pageIndex: pageNo,
        version: version,
        appKey: APP_KEY
    };
    return conditions;

}
function ajaxSearchAPKList(conditions) {
    console.log("ajaxSearchHospitalList");
    $.ajax({
        type: "GET",
        url: PROJECT_CTX + "/trafficback/selectVersion",
        data: conditions,
        dataType: "json",
        success: function (data) {
            console.log(data)
            var result = eval(data);
            var resultCode = result.status.code;
            console.log(resultCode)
            console.log(result.line);
            if (1 == resultCode) {
                //清空当前数据
                var APKList = $("#APKList").empty();
                //填写查询出来的数据
                $.each(result.line, function (i, item) {
                    var tr = "<tr>";

                    tr += "<td >";
                    tr += item.version;
                    tr += "</td>"

                    tr += "<td >";
                    tr += item.packagePath;
                    tr += "</td>"

                    tr += "<td >";
                    tr += item.createTime;
                    tr += "</td>"

                    /* tr += "<td >";
                     tr += " <a title='修改' href='#'>修改</a>";
                     tr += " <a title='删除' href='#'>删除</a>";
                     tr += "</td >";*/

                    tr += "</tr>";
                    APKList.append(tr);
                    //更新分页栏
                    reloadPage(result);
                });
            }

        }

        ,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest.status);
            console.log(XMLHttpRequest.readyState);
            console.log(textStatus);
        }
    });
}


function formatDate(date) {
    var date = new Date(date);
    var month = date.getMonth() + 1;
    if (month < 10) {
        month = "0" + month;
    }
    var today = date.getDate();
    if (today < 10) {
        today = "0" + today;
    }
    return date.getFullYear() + "-" + month + "-" + today;
}